package com.chinatsp.util;


import com.chinatsp.entity.AuthInfo;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chenzhengdong on 2017/3/29.
 */
public class StringUtils {

    private static final String OK_JSON="{\"resp_status\":\"OK\"}";
    private static SimpleDateFormat formatDef = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    private static final Pattern vehNoPattern = Pattern.compile("^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$");

    /**
     *
     * @param format
     *            如：yyyyMMdd hh:mm:ss
     * @return
     */
    public static String getDateString(Date date, String format) {
        if (isEmpty(format)) {
            return formatDef.format(date);
        } else {
            SimpleDateFormat formatA = new SimpleDateFormat(format);
            return formatA.format(date);
        }
    }

    public static boolean checkVehNo(String vehNo){
        if(vehNo==null)return false;
        Matcher isVehNo = vehNoPattern.matcher(vehNo);
        return isVehNo.matches();
    }


    /**
     * 从List里取第一条记录，类型必须为Map
     *
     * @param list
     * @return
     */
    public static Map<?, ?> getFirstFromList(List<?> list) {
        Map<?, ?> map = null;
        if (list != null && list.size() >= 1) {
            Object object = list.get(0);
            if (object != null && object instanceof Map) {
                map = (Map<?, ?>) object;
            }
        }
        return map;
    }

    /**
     * 从List里取第一条记录，类型必须为Map
     *
     * @param list
     * @return
     */
    public static Map<String, Object> removeFirstFromList(List<Map<String, Object>> list) {

        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.remove(0);
    }

    /**
     * 是否为空
     * 对map的判断
     * @param s
     * @return
     */
    public static boolean isEmpty(Object s) {
        if (s == null || s.toString().length() == 0 ||"null".equals(s.toString().trim().toLowerCase())) {
            return true;
        }else if(s instanceof Map<?,?> && ((Map<?,?>)s).size()==0){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断两个对象是否相等
     *
     * @param string
     * @param object
     * @return
     */
    public static boolean isEqualsIgnoreCase(String string, Object object) {
        if (string == null && object == null) {
            return true;
        } else if (string!=null&&string.equalsIgnoreCase(String.valueOf(object))) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 取cookie值
     * @param request
     * @return
     */
    public static AuthInfo getAccessToken(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        AuthInfo auth = new AuthInfo();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("customer_key".equals(cookie.getName())) {
                    auth.setCustomer_key(cookie.getValue());
                } else if ("access_token".equals(cookie.getName())) {
                    auth.setAccess_token(cookie.getValue());
                }
            }
        }
        return auth;
    }

    /**
     * 获取HttpRequest参数
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getQueryString(HttpServletRequest request) throws UnsupportedEncodingException {
        StringBuffer sb = new StringBuffer();
        if(request==null){
            //		}else if("get".equalsIgnoreCase(request.getMethod())){
            //		    sb.append(request.getQueryString());
        }else{
            Enumeration<?> params = request.getParameterNames() ;
            sb.append(request.getRequestURI());
            while(params.hasMoreElements())
            {
                String param = (String)params.nextElement();
                if(sb.toString().contains("?")){
                    sb.append("&");
                }else{
                    sb.append("?");
                }

                sb.append(param).append("=").append(request.getParameter(param));
            }
            sb.append("#\t").append(getAccessToken(request).toString()).append("#\t").append(request.getMethod());
        }
        return sb.toString();
    }

}
